<?php

namespace App\Modules\Apicrzs\Services\WeChat;

use App\Library\Wechat\PayWeChatApi;
use App\Modules\Apicrzs\Services\BaseServices;
use EasyWeChat\Pay\Application as payApp;
use Illuminate\Support\Facades\Config;

class WechatPayService extends BaseServices
{

    // "jdRecharge": 骏达  消费

    protected $notify_url = "https://wxopen.grzq.xyz/api/apicrzs/pay/notify/V3wechat" ;

    public function wx_pay($type,$param)
    {
        $amount = intval($param['amount']*100);
        $description = $param['description']??"";
        $open_id=$param['openid']??"";
        $order_sn=$param['order_sn']??"";
        $attach=$param['attach']??"";
        if ($open_id) {
            $wechat_config = getWxConfig('MiniApp');
            // 创建微信支付对象
            $wechat = new payApp($wechat_config);
            // 第一步：JSAPI 下单，组装请求参数
            $pay_params = [
                'description' => $description,                                     // 商品描述
                'out_trade_no' => $order_sn,                 // 商户订单号
                'attach' => json_encode($attach),                                        // 附加数据
                'notify_url' => $this->notify_url,      // 支付结果通知网址，如果不设置则会使用配置里的默认地址
                "amount" => [
                    "total" => $amount,                                                   // 订单总金额，单位为分，只能为整数
                    "currency" => "CNY"                                             // 货币类型，默认人民币：CNY，目前只支持该类型
                ],
                "payer" => [
                    "openid" => $open_id
                ]
            ];
            $result = PayWeChatApi::payPostJson($pay_params);
            if(isset($result['code'])){
                return ['code' => 0, 'data' => '', 'msg' => $result['msg']];
            }
            return $result;
        } else {
            return ['code' => 0, 'data' => '', 'msg' =>'缺少参数'  ];
        }
    }
    public function createNonceStr($length=10)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return date('Ymd') . $str;
    }

    /**
     * 微信 退款
     * @return voi
     */
    public function pay_refunds($param)
    {
        $out_trade_no=$param['order_sn'];
        $total=$param['refund_amount'];
        $out_refund_no=$param['out_refund_no'].rand_str(10,0);
        $refunds_params=[
            // 'transaction_id'=>$out_trade_no,
            'out_trade_no'=>$out_trade_no,
            'out_refund_no'=>$out_refund_no,
            "amount"=>[
                "refund"=> $total*100,
                'total'=>$total*100,                                                  // 订单总金额，单位为分，只能为整数
                "currency" => "CNY"
            ]

        ];
        $wechat_config = getWxConfig('MiniApp');
        // 创建微信支付对象
        $wechat = new payApp($wechat_config);
        $refunds_response = $wechat->getClient()->postJson("/v3/refund/domestic/refunds",$refunds_params);
        $refunds_result = $refunds_response->toArray(false);

        return   $refunds_result;
    }

}
